﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KBEngine.Common
{
    public class FixedMsg
    {
        static Dictionary<string, Message>  dicNameByMsg = new Dictionary<string, Message>();
        public static Message Get(string funcName)
        {
            Init(string.Empty);
            dicNameByMsg.TryGetValue(funcName, out var msg);
            return msg;
        }
        public static Message Get(ushort id)
        {
            Init(string.Empty);
            foreach (var item in dicNameByMsg)
            {
                if (item.Value.id == id)
                    return item.Value;
            }
            return null;
        }

        static bool isInit = false;

        /// <summary>
        /// flaot:可读取协议定义文件来验证哪些消息可用,暂未实现
        /// </summary>
        protected static void Init(string fixedMessageXML)
        {
            if (isInit)
                return;
            isInit = true;

            //kbe/res/server/messages_fixed_defaults.xml
            Message[] msgArray = new Message[] {
                new Message(){id = 1, msglen = -1, name = "Machine::onFindInterfaceAddr" },
                new Message() { id = 2, msglen = -1, name = "Machine::startserver" },
                new Message() { id = 3, msglen = -1, name = "Machine::stopserver" },
                new Message() { id = 4, msglen = -1, name = "Machine::onQueryAllInterfaceInfos" },
                new Message() { id = 5, msglen = -1, name = "Machine::onQueryMachines" },
                new Message() { id = 6, msglen = -1, name = "Machine::killserver" },
                new Message() { id = 701, msglen = 0, name = "Logger::onAppActiveTick" },
                new Message() { id = 702, msglen = -1, name = "Logger::registerLogWatcher" },
                new Message() { id = 703, msglen = -1, name = "Logger::deregisterLogWatcher" },
                new Message() { id = 704, msglen = -1, name = "Logger::writeLog" },
                new Message() { id = 41001, msglen = -1, name = "Baseapp::queryWatcher" },
                new Message() { id = 41002, msglen = -1, name = "Cellapp::queryWatcher" },
                new Message() { id = 41003, msglen = -1, name = "Loginapp::queryWatcher" },
                new Message() { id = 41004, msglen = -1, name = "Baseappmgr::queryWatcher" },
                new Message() { id = 41005, msglen = -1, name = "Cellappmgr::queryWatcher" },
                new Message() { id = 41006, msglen = -1, name = "Dbmgr::queryWatcher" },
                new Message() { id = 41007, msglen = -1, name = "Interfaces::queryWatcher" },
                new Message() { id = 41008, msglen = -1, name = "Logger::queryWatcher" },
                new Message() { id = 50001, msglen = -1, name = "Baseappmgr::queryAppsLoads" },
                new Message() { id = 50002, msglen = -1, name = "Cellappmgr::queryAppsLoads" },
                new Message() { id = 50003, msglen = -1, name = "Cellappmgr::querySpaces" },
                new Message() { id = 50004, msglen = -1, name = "Cellappmgr::setSpaceViewer" },
                new Message() { id = 50005, msglen = -1, name = "Cellapp::setSpaceViewer" },
                new Message() { id = 55001, msglen = -1, name = "Baseapp::onExecScriptCommand" },
                new Message() { id = 55002, msglen = -1, name = "Cellapp::onExecScriptCommand" },
                new Message() { id = 55003, msglen = -1, name = "Bots::onExecScriptCommand" },
            };
            foreach (var item in msgArray)
            {
                dicNameByMsg.Add(item.name, item);
            }
        }
    }
}
